package com.splant.android.smart.UILevel.Community;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.splant.android.R;
import com.splant.android.smart.ModelLevel.DetectorListBean;
import com.splant.android.smart.UILevel.Community.fragments.Garden_DetectorList_Adapter;

import java.util.List;

/**
 * Created by Administrator on 2017/8/12.
 */

public class Garden_Detector_Adapter extends RecyclerView.Adapter<Garden_Detector_Adapter.ViewHolder>   {

    private LayoutInflater mInflater;
    private List<DetectorListBean.Detector> detectorList;
    private Context mContext;
    public Garden_Detector_Adapter(Context context, List<DetectorListBean.Detector> detectorList)
    {
        mInflater = LayoutInflater.from(context);
        mContext=context;
        this.detectorList=detectorList;
    }
    @Override
    public Garden_Detector_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = mInflater.inflate(R.layout.garden_detector_adapter_item,viewGroup, false);

        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.img_detector= (ImageView) view.findViewById(R.id.img_detector);
        viewHolder.tv_detectorName= (TextView) view.findViewById(R.id.tv_detectorName);
        viewHolder.ll_bg= (LinearLayout) view.findViewById(R.id.ll_bg);

        return viewHolder;
    }

    private int selectIndex;
    @Override
    public void onBindViewHolder(Garden_Detector_Adapter.ViewHolder holder, final int position) {

        Glide.with(mContext)
                .load(detectorList.get(position).getPlantImgUrl())
                .dontAnimate()
                .placeholder(R.mipmap.garden_ic_plantimg_fail)
                .into(holder.img_detector);

        holder.tv_detectorName.setText(detectorList.get(position).getDetectorName());

        if (detectorList.get(position).getAbnState()==0)
            holder.tv_detectorName.setBackgroundResource(R.color.garden_plant_normal);
        else
            holder.tv_detectorName.setBackgroundResource(R.color.garden_plant_abnormal);

        holder.img_detector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null)
                {
                    mListener.onItemChick(position);
                    selectIndex=position;
                }
            }
        });

        if (position==selectIndex)
            holder.ll_bg.setBackgroundResource(R.color.garden_color0);
        else
            holder.ll_bg.setBackgroundResource(R.color.garden_color1);

    }
    @Override
    public int getItemCount() {
        return detectorList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }
        ImageView img_detector;
        TextView  tv_detectorName;
        LinearLayout ll_bg;
    }

    private onItemClickListener mListener = null;
    public void setOnItemClickListener(onItemClickListener mListener)
    {
        this.mListener=mListener;
    }
    public interface onItemClickListener {
        void onItemChick(int pos);
    }

}
